package com.lang.dao.impl;

import com.lang.dao.UserDao;
import com.lang.entity.User;
import com.lang.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/*
 * @Date : 2024/3/12 16:08
 * @Created by Lang
 * @ProjectName : LclShop
 */
public class UserDaoImpl implements UserDao {
    public QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());

    @Override
    public User getUserByName(String username) {
        String sql = "select * from user where u_name = ?";
        try {
            return runner.query(sql, new BeanHandler<>(User.class), username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        String sql = "insert into user(u_name,u_password,u_email,u_sex,u_status,u_code,u_role) values(?,?,?,?,?,?,?)";

        try {
            return runner.update(sql, user.getU_name(), user.getU_password(), user.getU_email(), user.getU_sex(), user.getU_status(), user.getU_code(), user.getU_role());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User getUserByCode(String code) {
        String sql = "select * from user where u_code=?";
        try {
            return runner.query(sql,new BeanHandler<>(User.class),code);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateStatus(int uId) {
        String sql = "update user set u_status = 1 where u_id = ?";
        try {
            return runner.update(sql, uId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public User adminLogin(String username) {
        String sql = "select * from user where u_name = ? and u_role = 1";
        try {
            return runner.query(sql, new BeanHandler<>(User.class), username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> list() {
        String sql = "select * from user";
        try {
            return runner.query(sql, new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> searchByGender(String gender) {
        String sql = "select * from user where u_sex = ?";
        try {
            return runner.query(sql, new BeanListHandler<>(User.class), gender);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> searchByUsername(String username) {
        String sql = "select * from user where u_name like ?";
        try {
            return runner.query(sql, new BeanListHandler<>(User.class), "%"+username+"%");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> search(String username, String gender) {
        String sql = "select * from user where u_name like ? and u_sex = ?";
        try {
            return runner.query(sql,new BeanListHandler<>(User.class),"%"+username+"%",gender);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int deleteUser(String id) {
        String sql = "delete from user where u_id = ?";
        try {
            return runner.update(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
